package components.dialogs
{
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import flash.utils.clearInterval;
	import flash.utils.setInterval;

	

	public class clt_waitAlert extends clt_alertInvite {
		private var _descText		: String;
		private var _secCount		: int;
		private var _endCount		: int;
		private var _timer			: int;
		private var _completeFunc	: Function;
		public function clt_waitAlert(parent:uif_component, message:String, secCount: int, basnnerClass: Class, closeFunc:Function=null, completeFunc:Function=null) {
			super(parent, message, basnnerClass, closeFunc, true);
			title.text = app.classes.locale.WAITTITLE;
			_timer = setInterval(doTimer, 1000);
			_secCount = secCount;
			_endCount = curSec + secCount;
			_completeFunc = completeFunc;
			_descText = message;
			updateDesc();
		}

		private function clearTimer(): void {
			if (_timer) {
				clearInterval(_timer);
				_timer = 0;
			}
		}
		
		override public function dispose():void {
			clearTimer();
			super.dispose();
		}
		
		private function doTimerComplete(): void {
			clearTimer();
			if (_completeFunc != null) _completeFunc();
		}
		
		private function doTimer(): void {
			_secCount = _endCount - curSec;
			if (_secCount <= 0) doTimerComplete();
			else updateDesc();
		}
		
		protected function get curSec(): int {
			var _date: Date = new Date();
			return Math.round(_date.time / 1000);
		}
		
		private function updateDesc(): void {
			_desc.textField.htmlText = _descText.replace('%s', uif_util.timeToStr(_secCount, 1));
		}
		
	}
}